package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.pay.PayResult;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.yizhibo.Constants;
import com.xgkj.diyiketang.yizhibo.PlayerActivity;
import com.xgkj.diyiketang.yizhibo.PlayerNewActivity;
import com.xgkj.diyiketang.yizhibo.RecorderActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/8/30.
 */
public class MyZhiboAdapter extends BaseAdapter{
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();
    public MyZhiboAdapter(Context context) {
        mContext = context;
    }
    private String state;

    public void updateData(List<EatShowUserInfo> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList.size()>0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList.size()>0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.myzhibo_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews(mList.get(position), (ViewHolder) convertView.getTag());

        if (state != null && state.length() > 0) {
            if (state.equals("5")) {

                //childHolder.liveState.setText("预约中");
                final EatShowUserInfo info = mList.get(position);
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt), mContext.getString(R.string.if_tiqian_live), mContext.getString(R.string.ok), mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String zhiboid = info.getId();
                                String titleString = info.getLiveTitle();
                                Intent intent = new Intent(mContext, RecorderActivity.class);
                                intent.putExtra("TITLE", titleString);
                                intent.putExtra("ID", zhiboid);
                                mContext.startActivity(intent);
                            }
                        },null);

                    }
                });
            } else if (state.equals("1")) {
               // childHolder.liveState.setText("回放");
                final EatShowUserInfo info = mList.get(position);
                final int currentPosition = position;
                final ArrayList<EatShowUserInfo> myzhiboList = (ArrayList<EatShowUserInfo>) mList;
                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String vid = info.getVid();
                        String liveState = info.getLiveState();
                        String isPlayer = info.getIsMoive2();
                        String isuse = info.getIsuse();
                        Intent intent = new Intent(mContext, PlayerNewActivity.class);
                        intent.putExtra(Constants.EXTRA_KEY_VIDEO_ID, vid);
                        intent.putExtra("isUse",isuse);
                        intent.putExtra(Constants.ZHIBO_PLAY,isPlayer);
                        intent.putExtra(Constants.LIVE_STATE, liveState);
                        intent.putExtra("ZHIBO_LIST", myzhiboList);
                        intent.putExtra("CURRENT_POSITION",currentPosition);
                        intent.putExtra("type","Myzhibo");
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                });
            }
        }


        return convertView;
    }

    private OnAdapterClickListener mListener;
    public void setOnAdapterClickListener(OnAdapterClickListener listener){
        mListener = listener;
    }

    public ArrayList<EatShowUserInfo> getDataList() {
        return (ArrayList<EatShowUserInfo>) mList;
    }

    public interface OnAdapterClickListener{
       public void onClick(EatShowUserInfo info);
    }


    private void initializeViews(final EatShowUserInfo info, ViewHolder childHolder) {
        if (info != null) {
            String anchorImgUrl = info.getUserLogo();
            if (anchorImgUrl != null && anchorImgUrl.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_40(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(anchorImgUrl, size));
                if (cacheBitmap != null) {
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, anchorImgUrl, size);
                }
            }
            if (info.getNickName() != null) {
                try {
                    childHolder.nickname.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }


            childHolder.time.setText(info.getLiveTime());

            String fengmian = info.getLiveCoverUrl();
            if (fengmian != null && fengmian.length() > 0) {
                ViewUtil.Size size = ViewUtil.getHeadImageSize_375(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(fengmian, size));
                if (cacheBitmap != null) {
                    childHolder.fengmian.setImageBitmap(cacheBitmap);
                } else {
                    childHolder.fengmian.setImageResource(R.drawable.zhanweitu);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.fengmian, fengmian, size);
                }
            }

            childHolder.zhiboDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt), mContext.getString(R.string.you_sure_delete_video), mContext.getString(R.string.ok), mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (mListener != null){
                                mListener.onClick(info);
                            }
                        }
                    },null);
                }
            });

            state = info.getLiveState();
            if (state != null && state.length() > 0) {
                if (state.equals("5")) {
                    childHolder.zhiboDelete.setVisibility(View.GONE);
                    childHolder.liveState.setText("预约中");
                } else if (state.equals("1")) {
                    childHolder.liveState.setText("回放");
                }else if(state.equals("6")){
                    childHolder.liveState.setText("已过期");
                }
            }

            if(info.getLiveTitle()!=null){
                try {
                    childHolder.liveTitle.setText(URLDecoder.decode(info.getLiveTitle(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    public class ViewHolder {
        private RoundImageView headImg;
        private TextView nickname;
        private TextView time;
        private ImageView fengmian;
        private ImageView zhiboDelete;
        private TextView liveTitle;
        private TextView liveState;

        public ViewHolder(View view) {
            headImg = (RoundImageView) view.findViewById(R.id.head_img);
            nickname = (TextView) view.findViewById(R.id.nickname);
            time = (TextView) view.findViewById(R.id.time);
            fengmian = (ImageView) view.findViewById(R.id.fengmian);
            zhiboDelete = (ImageView) view.findViewById(R.id.zhibo_delete);
            liveTitle = (TextView) view.findViewById(R.id.live_title);
            liveState = (TextView) view.findViewById(R.id.state);
        }
    }
}
